﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

using CE.User;
using CE.Global;
using CE.Database;

public partial class management_User_usersummary_report : System.Web.UI.Page
{
    string PageCode = "CXRPTCIQTURSM";
    DataSet _dsSummary = new DataSet();
    DataTable _dtULGSummary = new DataTable();
    DataTable _dtURGSummary = new DataTable();
    DataTable _dtCRVSummary = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
            }
            trResultCount.Visible = false;
            lblError.Text = "";
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();

            int _cryr = CE.Global.General.GetUTCDateTime().Year;
            int _styr = 2010;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
                ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");

            lblLoginTotalCount.Text = "";
            lblRegisterTotalCount.Text = "";
            lblCRVTotalCount.Text = "";
            lblLoginCount.Visible = false;
            lblRegisterCount.Visible = false;
            lblCRVCount.Visible = false;
            lblLoginRecordCount.Text = "";
            lblRegisterRecordCount.Text = "";
            lblCRVRecordCount.Text = "";
            divULG.Visible = false;
            divURG.Visible = false;
            divCRV.Visible = false;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Cache.Remove("ULGResult");
        ViewState["ULGSortExpression"] = "LoginDate";
        ViewState["ULGSortOrder"] = " ASC";

        Cache.Remove("URGResult");
        ViewState["URGSortExpression"] = "RegistrationDate";
        ViewState["URGSortOrder"] = " ASC";

        Cache.Remove("CRVResult");
        ViewState["CRVSortExpression"] = "CRVDate";
        ViewState["CRVSortOrder"] = " ASC";

        LoadGrid();

    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "";
        ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = "From Date :";
            lblToDate.Text = " To Date :";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void grvLoginSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int _Count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RecordCount"));
            int _RecordCount = Convert.ToInt32(lblLoginTotalCount.Text);
            _RecordCount = _RecordCount + _Count;
            lblLoginTotalCount.Text = _RecordCount.ToString();
        }
    }

    protected void grvRegisterSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int _Count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RecordCount"));
            int _RecordCount = Convert.ToInt32(lblRegisterTotalCount.Text);
            _RecordCount = _RecordCount + _Count;
            lblRegisterTotalCount.Text = _RecordCount.ToString();
        }
    }

    protected void grvCarResultViewSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int _Count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RecordCount"));
            int _RecordCount = Convert.ToInt32(lblCRVTotalCount.Text);
            _RecordCount = _RecordCount + _Count;
            lblCRVTotalCount.Text = _RecordCount.ToString();
        }
    }

    public bool ValidDate(Int32 Year, Int32 Month, Int32 Day)
    {
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    public bool Validatioin()
    {
        lblError.Text = "";
        bool _isValid = true;
        DateTime _fromDate, _toDate;
        if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            if (ddlDay1.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (_fromDate > CE.Global.General.GetUTCDateTime())
                    {
                        _isValid = false;
                        lblError.Text = "Date should be less than or equal to Today";
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = "Select Valid Date";
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = "Select Date";
            }
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            if (ddlDay1.SelectedIndex >= 0 && ddlDay2.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (ValidDate(Convert.ToInt32(ddlYear2.Text), Convert.ToInt32(ddlMonth2.Text), Convert.ToInt32(ddlDay2.Text)))
                    {
                        _toDate = Convert.ToDateTime(Convert.ToInt32(ddlYear2.Text) + "/" + Convert.ToInt32(ddlMonth2.Text) + "/" + Convert.ToInt32(ddlDay2.Text));
                        if (_fromDate <= CE.Global.General.GetUTCDateTime())
                        {
                            if (_fromDate > _toDate)
                            {
                                _isValid = false;
                                lblError.Text = "From date should be less than To date";
                            }
                        }
                        else
                        {
                            _isValid = false;
                            lblError.Text = "From date should be less than Today";
                        }
                    }
                    else
                    {
                        _isValid = false;
                        lblError.Text = "Select Valid To date";
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = "Select Valid From date";
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = "Select From date and To date";
            }
        }
        return _isValid;
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }
            UserSummaryDB _usersummarydb = new UserSummaryDB();
            string _SummaryType = ddlSummaryType.Text;
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate1 = txtReportDate1.Text.Trim();
            string _ReportDate2 = txtReportDate2.Text.Trim();
            _dsSummary = _usersummarydb.GetUserSummaryCount(_SummaryType, _ReportType, _ReportDate1, _ReportDate2);
            if (_dsSummary != null && _dsSummary.Tables.Count > 0)
            {
                if (_SummaryType == "All")
                {
                    _dtULGSummary = _dsSummary.Tables[0];
                    _dtURGSummary = _dsSummary.Tables[1];
                    _dtCRVSummary = _dsSummary.Tables[2];

                    divULG.Visible = true;
                    divURG.Visible = true;
                    divCRV.Visible = true;
                }
                else if (_SummaryType == "ULG")
                {
                    _dtULGSummary = _dsSummary.Tables[0];
                    _dtURGSummary = null;
                    _dtCRVSummary = null;

                    divULG.Visible = true;
                    divURG.Visible = false;
                    divCRV.Visible = false;
                }
                else if (_SummaryType == "URG")
                {
                    _dtURGSummary = _dsSummary.Tables[0];
                    _dtULGSummary = null;
                    _dtCRVSummary = null;
                    divULG.Visible = false;
                    divURG.Visible = true;
                    divCRV.Visible = false;
                }
                else if (_SummaryType == "CRV")
                {
                    _dtCRVSummary = _dsSummary.Tables[0];
                    _dtURGSummary = null;
                    _dtULGSummary = null;
                    divULG.Visible = false;
                    divURG.Visible = false;
                    divCRV.Visible = true;
                }

                if (_dtULGSummary != null && _dtULGSummary.Rows.Count > 0)
                {
                    lblError.Text = "";
                    lblLoginTotalCount.Text = "0";
                    lblLoginCount.Visible = true;
                    DataView dvCI = new DataView((DataTable)_dtULGSummary);
                    dvCI.Sort = ViewState["ULGSortExpression"].ToString() + ViewState["ULGSortOrder"].ToString();
                    grvLoginSummary.DataSource = dvCI;
                    grvLoginSummary.DataBind();
                    lblLoginRecordCount.Text = grvLoginSummary.Rows.Count.ToString() + " record(s).";
                }
                else
                {
                    grvLoginSummary.DataSource = null;
                    grvLoginSummary.DataBind();
                    lblLoginRecordCount.Text = "";
                    lblLoginTotalCount.Text = "";
                    lblLoginCount.Visible = false;
                }
                if (_dtURGSummary != null && _dtURGSummary.Rows.Count > 0)
                {
                    lblError.Text = "";
                    lblRegisterTotalCount.Text = "0";
                    lblRegisterCount.Visible = true;
                    DataView dvPA = new DataView((DataTable)_dtURGSummary);
                    dvPA.Sort = ViewState["URGSortExpression"].ToString() + ViewState["URGSortOrder"].ToString();
                    grvRegisterSummary.DataSource = dvPA;
                    grvRegisterSummary.DataBind();
                    lblRegisterRecordCount.Text = grvRegisterSummary.Rows.Count.ToString() + " record(s).";
                }
                else
                {
                    grvRegisterSummary.DataSource = null;
                    grvRegisterSummary.DataBind();
                    lblRegisterRecordCount.Text = "";
                    lblRegisterTotalCount.Text = "";
                    lblRegisterCount.Visible = false;
                }
                if (_dtCRVSummary != null && _dtCRVSummary.Rows.Count > 0)
                {
                    lblError.Text = "";
                    lblCRVTotalCount.Text = "0";
                    lblCRVCount.Visible = true;
                    DataView dvTI = new DataView((DataTable)_dtCRVSummary);
                    dvTI.Sort = ViewState["CRVSortExpression"].ToString() + ViewState["CRVSortOrder"].ToString();
                    grvCarResultViewSummary.DataSource = dvTI;
                    grvCarResultViewSummary.DataBind();
                    lblCRVRecordCount.Text = grvCarResultViewSummary.Rows.Count.ToString() + " record(s).";
                }
                else
                {
                    grvCarResultViewSummary.DataSource = null;
                    grvCarResultViewSummary.DataBind();
                    lblCRVRecordCount.Text = "";
                    lblCRVTotalCount.Text = "";
                    lblCRVCount.Visible = false;
                }
            }
            else
            {
                grvLoginSummary.DataSource = null;
                grvLoginSummary.DataBind();

                grvRegisterSummary.DataSource = null;
                grvRegisterSummary.DataBind();

                grvCarResultViewSummary.DataSource = null;
                grvCarResultViewSummary.DataBind();

                lblLoginTotalCount.Text = "";
                lblRegisterTotalCount.Text = "";
                lblCRVTotalCount.Text = "";
                lblLoginCount.Visible = false;
                lblRegisterCount.Visible = false;
                lblCRVCount.Visible = false;
                lblLoginRecordCount.Text = "";
                lblRegisterRecordCount.Text = "";
                lblCRVRecordCount.Text = "";
            }

            if (_SummaryType == "All")
            {
                trResultCount.Visible = true;
                DataSet _dsResultCount = new DataSet();
                _dsResultCount = _usersummarydb.GetUserResultCount(_ReportType, _ReportDate1, _ReportDate2);
                if (_dsResultCount != null && _dsResultCount.Tables.Count > 0 && _dsResultCount.Tables[0].Rows.Count > 0)
                {
                    grvResultCount.Visible = true;
                    grvResultCount.DataSource = _dsResultCount;
                    grvResultCount.DataBind();
                }
                else
                {
                    grvResultCount.Visible = false;
                    grvResultCount.DataSource = null;
                    grvResultCount.DataBind();
                }
            }
            else
            {
                trResultCount.Visible = false;
                grvResultCount.Visible = false;
                grvResultCount.DataSource = null;
                grvResultCount.DataBind();
            }
        }
        else
        {
            trResultCount.Visible = false;
            grvLoginSummary.DataSource = null;
            grvLoginSummary.DataBind();

            grvRegisterSummary.DataSource = null;
            grvRegisterSummary.DataBind();

            grvCarResultViewSummary.DataSource = null;
            grvCarResultViewSummary.DataBind();

            lblLoginTotalCount.Text = "";
            lblRegisterTotalCount.Text = "";
            lblCRVTotalCount.Text = "";
            lblLoginCount.Visible = false;
            lblRegisterCount.Visible = false;
            lblCRVCount.Visible = false;
            lblLoginRecordCount.Text = "";
            lblRegisterRecordCount.Text = "";
            lblCRVRecordCount.Text = "";

            divULG.Visible = false;
            divURG.Visible = false;
            divCRV.Visible = false;
        }
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
    }

}
